package com.changgou.goods.controller;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.SpuService;
import com.changgou.goods.pojo.Spu;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin
@RequestMapping("/spu")
public class SpuController {


    @Autowired
    private SpuService spuService;

    /**
     * 添加商品第三步
     * @param spu
     * @return
     */
    @PutMapping
    public Result CommitSpu(@RequestBody Spu spu){
        spuService.commitSpu(spu);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 设置主图
     * @param id
     * @param image
     * @return
     */
    @PutMapping("/{id}/{image}")
    public Result updateImage(@PathVariable String id,@PathVariable String image){
        spuService.updateImage(id,image);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 商品分页条件查询
     */
    @GetMapping(value = "/findPage/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap,@PathVariable("page")int page,@PathVariable("size") int size){
        Page pageInfo = spuService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    @GetMapping("/findState")
    public Result findState(){
            List<Map<String,Object>> all = spuService.findAll();
        return new Result(true,StatusCode.OK,"查询成功",all);
    }
}
